package cn.hahah.module.blog.service.articletag;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.articletag.vo.*;
import cn.hahah.module.blog.dal.dataobject.articletag.ArticleTagDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.articletag.ArticleTagMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 文章标签 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ArticleTagServiceImpl implements ArticleTagService {

    @Resource
    private ArticleTagMapper articleTagMapper;

    @Override
    public Long createArticleTag(ArticleTagSaveReqVO createReqVO) {
        // 插入
        ArticleTagDO articleTag = BeanUtils.toBean(createReqVO, ArticleTagDO.class);
        articleTagMapper.insert(articleTag);
        // 返回
        return articleTag.getId();
    }

    @Override
    public void updateArticleTag(ArticleTagSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleTagExists(updateReqVO.getId());
        // 更新
        ArticleTagDO updateObj = BeanUtils.toBean(updateReqVO, ArticleTagDO.class);
        articleTagMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticleTag(Long id) {
        // 校验存在
        validateArticleTagExists(id);
        // 删除
        articleTagMapper.deleteById(id);
    }

    private void validateArticleTagExists(Long id) {
        if (articleTagMapper.selectById(id) == null) {
            throw exception(ARTICLE_TAG_NOT_EXISTS);
        }
    }

    @Override
    public ArticleTagDO getArticleTag(Long id) {
        return articleTagMapper.selectById(id);
    }

    @Override
    public PageResult<ArticleTagDO> getArticleTagPage(ArticleTagPageReqVO pageReqVO) {
        return articleTagMapper.selectPage(pageReqVO);
    }

}